package pl.pawluk.inz.dao.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: mandp
 * Date: 23.05.13
 * Time: 15:45
 * To change this template use File | Settings | File Templates.
 */
public class AbstractService<DTO, ENT> implements Serializable {
    private final Class<DTO> dtoClass;
    private final Class<ENT> entClass;

    AbstractService(Class<DTO> dtoClass, Class<ENT> entClass) {
        this.dtoClass = dtoClass;
        this.entClass = entClass;
    }

    @Autowired
    private ConversionService conversionService;

    protected DTO convertToDTO(ENT o) {
        return conversionService.convert(o, dtoClass);
    }

    protected ENT convertToENT(DTO o) {
        return conversionService.convert(o, entClass);
    }

    protected void convertToDTO(Collection<? extends ENT> set, Collection<DTO> res) {
        if (set == null) {
            return;
        }
        for (Iterator<? extends ENT> iter = set.iterator(); iter.hasNext(); ) {
            res.add(convertToDTO(iter.next()));
        }
    }
    protected void convertToENT(Collection<? extends DTO> set, Collection<ENT> res) {
        if (set == null) {
            return;
        }
        for (Iterator<? extends DTO> iter = set.iterator(); iter.hasNext(); ) {
            res.add(convertToENT(iter.next()));
        }
    }


}
